package org.iori.luodan.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.iori.json.JSONHelper;
import org.iori.luodan.base.BaseLogicFactory;
import org.iori.luodan.logic.DeviceLogic;
import org.iori.luodan.logic.UserLogic;
import org.iori.luodan.base.BaseServlet;
import org.iori.luodan.entity.UserInfoEntity;

public class GetUserInfoServlet extends BaseServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 744315239588347942L;
	
	private static UserLogic userLogic = BaseLogicFactory.getUserLogic();
	private static DeviceLogic deviceLogic = BaseLogicFactory.getDeviceLogic();
    
	@Override
	protected String doExcute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		// currentPageを取得する
		String user_name = request.getParameter("user_name").toString();
		String password = request.getParameter("password").toString();
		Object str_md5 = request.getParameter("ismd5");
		Boolean isMd5 = true;
		if (null == str_md5) {
			isMd5 = false;
		}
		UserInfoEntity userInfoEntity = userLogic.getUserInfo(user_name, password, isMd5);
		
		Object obj_device_token = request.getParameter("device_token");
		if (null != obj_device_token) {
			String deviceToken = String.valueOf(obj_device_token);
			if (!"".equals(deviceToken)) {
				Object obj_userId = request.getParameter("user_id");
				int userId = -1;
				if (null != obj_userId) {
					userId = Integer.valueOf(String.valueOf(obj_userId).trim());
				}
				deviceLogic.insertOrReplaceDeviceInfo(deviceToken, userId);
			}
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		JSONHelper.writeObject(response, userInfoEntity);
		return null;
	}

}
